package com.mrtan.controller;

import cn.dev33.satoken.util.SaResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrtan.bean.Customer;
import com.mrtan.dto.PageBeanDto;
import com.mrtan.service.CustomerService;
import com.mrtan.vo.CustomerVO;
import com.mrtan.vo.CustomerWhereVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 客户
 * @author 唐博华
 * @version 2023/12/12
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Resource
    private CustomerService customerService;
    /**
     * 根据月份查询客户名及其订单数
     */

    @GetMapping("selectNames")
    public SaResult list() {
        List<String> names = customerService.selectNames();
        return SaResult.ok().setData(names);
    }
    @GetMapping("/namees")
    public SaResult list(Integer selected) {
        System.out.println(selected);
        List<Map<Customer, Integer>> customerOrderSum = customerService.getCustomerOrderSum(selected);
        System.out.println(customerOrderSum);
        return SaResult.ok().setData(customerOrderSum);
    }
    /**
     * 订单最近销售距离天数
     */


    @GetMapping("/list")
    public SaResult getTimeList() {
        List<Map<String, Object>> maps = customerService.selectTotalorderByCustomerTime();
        return SaResult.ok().setData(maps);
    }
    /**
     * 根据月份查询客户名及其订单总金额
     */


    @GetMapping("/price")
    public SaResult getPrice(Integer month) {
        List<Map<String, Object>> maps = customerService.selectTotalAmountByCustomerAndTime(month);
        return SaResult.ok().setData(maps);
    }

    /**
     * 查询用户订单数
     */

    @GetMapping("/orderCount")
    public SaResult getOrderCount(String customerName){
        int i = customerService.selectOrderCount(customerName);
        return SaResult.ok().setData(i);
    }

    /**
     * 查询所有客户(含模糊查询)
     *
     */
    @PostMapping("/allCustomer")
    public SaResult allCustomer(@RequestBody CustomerWhereVO customerWhereVO) {
        PageHelper.startPage(customerWhereVO.getPage(), customerWhereVO.getLimit());
        List<Customer> customers = customerService.selectAll(customerWhereVO);
        PageInfo<Customer> pageInfo = new PageInfo<>(customers);
        PageBeanDto<Customer> pageBeanDto = new PageBeanDto();
        pageBeanDto.setTotal((int) pageInfo.getTotal());
        pageBeanDto.setList(pageInfo.getList());
        return SaResult.ok().setData(pageBeanDto);
    }

    /**
     * 增加用户
     */


    @PostMapping("/addCustomer")
    public SaResult addCustomer(@RequestBody CustomerVO CustomerVO) {
        int i = customerService.insertSelective(CustomerVO);
        if(i>0){
            return SaResult.ok("添加成功");
        }
        else {
            return SaResult.error("添加失败");
        }
    }

    /**
     * 删除用户
     */

    @DeleteMapping("/deleteCustomer/{id}")
    public SaResult deleteCustomer(@PathVariable("id") Integer id) {
        customerService.deleteByPrimaryKey(id);
        return SaResult.ok();
    }

    /**
     * 修改用户
     */

    @PostMapping("/updateCustomer")
    public SaResult updateCustomer(@RequestBody CustomerVO CustomerVO) {
        System.out.println(1);
        customerService.updateByPrimaryKeySelective(CustomerVO);
        return SaResult.ok();
    }



}
